#!/bin/bash

cm_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
[ -d "$cm_dir" ] || {
	echo "FATAL: no current dir (maybe running in zsh?)"
	exit 1
}
TOP_DIR=$(realpath $cm_dir/..)

# shellcheck source=common.sh
source "$cm_dir/common.sh"

#################################################################################################

# arguments
AMB_OPER_IMAGE_FULL=${AMB_OPER_IMAGE_FULL:-$1}

# the output directory
ARTIFACTS_DIR=${ARTIFACTS_DIR:-$2}

#################################################################################################

helm_dir="$TOP_DIR/deploy/helm/ambassador-operator"

# the directory that contains the CRDs files
input_crds_dir=$TOP_DIR/deploy/crds

# output artifacts (manifests)
artifact_crds_manif="$ARTIFACTS_DIR/ambassador-operator-crds.yaml"
artifact_oper_manif="$ARTIFACTS_DIR/ambassador-operator.yaml"

# output helm manifests
helm_crds_manif="$helm_dir/crds/ambassador-installation.yaml"

# helm parameters
release_name="ambassador"
namespace="ambassador"
values_yaml="$helm_dir/values.yaml"
full_image=$AMB_OPER_IMAGE_FULL
args="-n ${namespace} --values $values_yaml --set image.name=${AMB_OPER_IMAGE_FULL}"

namespace_manif=$(
	cat <<EOF
---
# Source: generated by $0
apiVersion: v1
kind: Namespace
metadata:
  name: ${namespace}
EOF
)

# a temporary manifest
tmp_manif=/tmp/manif.yaml

#################################################################################################

info "Preparing release manifests in $ARTIFACTS_DIR"

[ -d "$helm_dir" ] || abort "helm directory ${helm_dir} does not exist"
[ -f "$values_yaml" ] || abort "no 'values.yaml' found at '$values_yaml'"
[ -n "$ARTIFACTS_DIR" ] || abort "no ARTIFACTS_DIR provided"
[ -d "$ARTIFACTS_DIR" ] || abort "'$ARTIFACTS_DIR' is not a valid directory"
[ -n "$AMB_OPER_IMAGE_FULL" ] || abort "no AMB_OPER_IMAGE_FULL provided"

rm -f $ARTIFACTS_DIR/*.yaml
mkdir -p "$(dirname $ARTIFACTS_DIR)"

info "Copying CRDs..."
mkdir -p "$(dirname $artifact_crds_manif)"
cat $input_crds_dir/*_crd.yaml >$artifact_crds_manif || abort "could not create $artifact_crds_manif"

# we must copy the CRD to the Helm chart
info "Updating CRD in Helm chart in $helm_crds_manif"
mkdir -p "$(dirname $helm_crds_manif)"
cp -f $artifact_crds_manif $helm_crds_manif || abort "could not create $helm_crds_manif"

# then we use the Helm chart for generating the standalone manifest, using
# the default values.yaml
info "Rendering Helm chart, using image '${AMB_OPER_IMAGE_FULL}'..."
mkdir -p "$(dirname $tmp_manif)"
helm template $release_name \
	$args --set deploymentTool=amb-oper-manifest --skip-crds $helm_dir >$tmp_manif || abort "could not create $tmp_manif"

info "Creating generic manifest, with namespace '${namespace}'..."
rm -f $artifact_oper_manif
mkdir -p "$(dirname $artifact_oper_manif)"
echo "$namespace_manif" >$artifact_oper_manif
cat "$tmp_manif" >>$artifact_oper_manif || abort "could not create $artifact_oper_manif"

# finally, we must generate manifests fort third-party projects (ie, KIND)
# by copying the regular manifest and adding some project-specific manifest
# (usually a customized AmbassadorInstallation)
info "Creating project-specific manifest..."
third_party_manifest_base=$ARTIFACTS_DIR/$(basename $artifact_oper_manif .yaml)
for d in deploy/third-party/*; do
	[ -d $d ] || continue
	db=$(basename $d)
	dest=${third_party_manifest_base}-${db}.yaml
	mkdir -p "$(dirname $dest)"
	info "... creating manifests for '$db'"
	cat $artifact_oper_manif $d/*.yaml >$dest || abort "could not create ${dest} for $db"
done

if [ -n "$VERBOSE" ]; then
	info "Showing manifests generated in $ARTIFACTS_DIR:"
	for i in $ARTIFACTS_DIR/*.yaml; do
		info "... $i"
		hl && cat $i && hl
	done
else
	info "Files generated in $ARTIFACTS_DIR: $(ls $ARTIFACTS_DIR | tr '\n' ' ')"
fi
